// pages/mineToCash/mineToCash.js
import {$digest, $init} from '../../lib/page.data'
import {$request, Session} from '../../lib/page.auth'

const {regeneratorRuntime} = global
//获取应用实例
let api = require('../../utils/api.js');
let http = require('../../utils/http.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        amount: 0,
        withdraw: 0,//提现金额,计算得到
        withdrawWithInterest: 0,
        alipay_account: '',//支付宝账号
        alipay_name: '',//支付宝账户提现真实姓名
        playList: [],
        playListInit: [],
        withdrawAll: false,//提现全部选项
        interest: 0.1,//提现手续费
        floor: 1,//提现门槛
        group: '测试分组'
    },
    //加载用户可提取金额的订单
    async getWithdrawableOrders() {
        var res = await $request({
            url: api.play_list,
            method: 'GET',
        });
        return JSON.parse(res.data);
    },
    //取消某一项可提现
    cancelWithdraw: function (e) {
        let dataset = e.currentTarget.dataset;
        let index = dataset.id;
        let price = dataset.price;
        this.data.playList.splice(index, 1);
        //改变总金额
        this.setData({
            withdraw: this.data.withdraw - price,
            playList: this.data.playList,
            withdrawAll: false
        });
        if (this.data.withdraw < 1) {
            this.setData({withdrawWithInterest: 0});
        } else {
            this.setData({withdrawWithInterest: Math.round(this.data.amount * (1 - this.data.interest) * 100) / 100});
        }
    },
    groupBlurHandler: function (e) {
        this.setData({
            group: e.detail.value
        });
    },
    accountBlurHandler: function (e) {
        this.setData({
            alipay_account: e.detail.value
        });
    },
    nameBlurHandler: function (e) {
        this.setData({
            alipay_name: e.detail.value
        })
    },
    //提现全部
    withdrawAll: function (e) {
        this.setData({
            withdrawAll: true,
            playList: this.data.playListInit,
            withdraw: this.data.amount,
        });
        if (this.data.amount < 1) {
            this.setData({withdrawWithInterest: 0});
        } else {
            this.setData({withdrawWithInterest: Math.round(this.data.amount * (1 - this.data.interest) * 100) / 100});
        }
    },
    toast: function (title, duration) {
        wx.showToast({
            title: title,
            icon: 'none',
            duration: 2000
        });
    },
    trim: function (str) {
        return str.replace(/^(\s|\xA0)+|(\s|\xA0)+$/g, '');
    },
    cancelTap: function () {
        wx.navigateBack({
            delta: 1
        })
    },
  bank_code: function (e) {
    this.data.bank_code = e.detail.value;
    },
  withdrawBlur: function (e) {
    this.data.amount = e.detail.value;
  },
  bank_name: function (e) {
    this.data.bank_name = e.detail.value;
  },
  bank_address: function (e) {
    this.data.bank_address = e.detail.value;
  },
    //发起提现
    async confirmTap(e) {
        if (this.data.amount == 0) {
            this.toast('当前可提现金额为0，不能发起提现');
            return false;
        }
      if (this.data.amount < this.data.floor) {
            this.toast('提现金额最少为' + this.data.floor + '元!');
            return false;
        }
      console.log(this.data.withdrawWithInterest);
      console.log(this.data.amount);
      if (parseFloat(this.data.withdrawWithInterest) < parseFloat(this.data.amount)) {
        this.toast('提现金额不可大于' + this.data.withdrawWithInterest + '可提现金额');
            return false;
        }
        //验证
      
      if (this.data.bank_code == "" || this.data.bank_code == null) {
        this.toast('请输入收款账号');
        return false;
      }
      if (this.data.bank_name == "" || this.data.bank_name == null) {
        this.toast('请输入收款银行');
        return false;
      }
      if (this.data.bank_address == "" || this.data.bank_address == null) {
        this.toast('请输入开户行地址');
        return false;
      }
      var that = this;
      console.log(wx.getStorageSync('user_info'));
      var data = {
        token: wx.getStorageSync('token'),
        'total': this.data.amount,
        'user_id': wx.getStorageSync('user_info').data.id,
        'bank_address': this.data.bank_address,
        'bank_code': this.data.bank_code,
        'bank_name': this.data.bank_name
      };
      http.post(api.withdraw, data, function (res) {
        if (res.data.code == 1) {
          wx.redirectTo({ url: '/pages/mineChange/mineChange?type=change' });
          return;
          setTimeout(function () {
            console.log(1111);
            var pages = getCurrentPages(); // 当前页面
            var beforePage = pages[pages.length - 2]; // 前一个页面
            wx.navigateBack({
              success: function () {
                beforePage.onLoad(); // 执行前一个页面的onLoad方法
              }
            });
          }, 1000)
        } else {
          
        }
      });
      return;
        var res = await $request({
            url: api.withdraw,
            method: 'POST',
            data: {
              
            }
        });
        console.log(res);
        this.toast(res.msg);
        if (res.code == 1) {
            //转向用户提现列表
            wx.redirectTo({url: '/pages/mineChange/mineChange?type=withdraw'});
          
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        $init(this);
      var token = wx.getStorageSync('token');
      var that=this;
      http.get(api.user_info, { token: token }, function (res) {
        if (res.data.code == 0) {
          util.msg('获取数据失败');
        }
        else {
          console.log(res);
          //刷新
          that.setData({
            withdrawAll: false,
            withdrawWithInterest: res.data.data.money,
            interest: res.data.data.balance_num/100,//提现手续费
            floor: res.data.data.min_balance,//提现门槛
            amount: res.data.data.money,
            realName: res.data.data.realName,
            bank_code: res.data.data.bank_code,
            bank_name: res.data.data.bank_name,
            bank_address: res.data.data.bank_address
          });
        }
      }, "请求中...");
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})